//
//  JYTimePickerView.m
//  JYFarm
//
//  Created by 花花 on 2017/10/26.
//  Copyright © 2017年 花花. All rights reserved.
//

#import "JYTimePickerView.h"
#import "Constants.h"
#import "PickerToolBar.h"
static CGFloat const PickerViewHeight = 240;
static CGFloat const PickerViewLabelWeight = 32;

@interface JYTimePickerView () <UIPickerViewDataSource, UIPickerViewDelegate>

@property (nonatomic, strong) UIPickerView *pickerView;

@property (nonatomic, strong) UIView *lineView;

@property(nonatomic,strong)NSArray *dataArr;

@property (nonatomic, strong, nullable) PickerToolBar *toolbar;


@property (nonatomic, strong, nullable)NSString *timeStr;

@end
@implementation JYTimePickerView




+ (instancetype)selectTime:(SelectTimeBlock)selectTimeBlock{
    JYTimePickerView *newPickerView = [[JYTimePickerView alloc] init];
    newPickerView.selectTimeBlock = selectTimeBlock;
    return newPickerView;
}


- (instancetype)init {
    self = [super init];
    if (self) {
        [self customUI];
        
        [self pickerView:self.pickerView didSelectRow:0 inComponent:0];
    
    }
    return self;
}

- (void)customUI {
    self.bounds = [UIScreen mainScreen].bounds;
    self.backgroundColor = RGBA(0, 0, 0, 102.0/255);
    [self.layer setOpaque:0.0];
    [self addSubview:self.pickerView];
    [self.pickerView addSubview:self.lineView];
    [self addSubview:self.toolbar];
    [self addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
}

- (UIView *)lineView {
    if (!_lineView) {
        _lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_Width, 0.5)];
        [_lineView setBackgroundColor:RGB(205, 205, 205)];
    }
    return _lineView;
}

- (UIPickerView *)pickerView
{
    if (!_pickerView) {
        CGFloat pickerW = Screen_Width;
        CGFloat pickerH = PickerViewHeight - 44;
        CGFloat pickerX = 0;
        CGFloat pickerY = Screen_Height + 44;
        _pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(pickerX, pickerY, pickerW, pickerH)];
        [_pickerView setBackgroundColor:[UIColor whiteColor]];
        [_pickerView setDataSource:self];
        [_pickerView setDelegate:self];
    }
    return _pickerView;
}

- (PickerToolBar *)toolbar {
    if (!_toolbar) {
        _toolbar = [[PickerToolBar alloc]initWithTitle:@""
                                     cancelButtonTitle:@"取消"
                                         okButtonTitle:@"确定"
                                             addTarget:self
                                          cancelAction:@selector(dismiss)
                                              okAction:@selector(selectedOk)];
        _toolbar.x = 0;
        _toolbar.y = Screen_Height;
    }
    return _toolbar;
}

- (void)selectedOk {
    if (self.selectTimeBlock) {
        self.selectTimeBlock(self.timeStr);
    }
    [self dismiss];
}



-(NSArray *)dataArr{

    if (!_dataArr) {
        
        _dataArr =@[@"7:00-8:00",@"8:00-9:00",@"9:00-10:00",@"10:00-11:00"];
        
    }

    return _dataArr;
}

- (void)show {
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [self setCenter:[UIApplication sharedApplication].keyWindow.center];
    [[UIApplication sharedApplication].keyWindow bringSubviewToFront:self];
    
    CGRect frameTool = self.toolbar.frame;
    frameTool.origin.y -= PickerViewHeight;
    
    CGRect framePicker =  self.pickerView.frame;
    framePicker.origin.y -= PickerViewHeight;
    [UIView animateWithDuration:0.33 animations:^{
        [self.layer setOpacity:1];
        self.toolbar.frame = frameTool;
        self.pickerView.frame = framePicker;
    } completion:^(BOOL finished) {
    }];
}

- (void)dismiss {
    CGRect frameTool = self.toolbar.frame;
    frameTool.origin.y += PickerViewHeight;
    
    CGRect framePicker =  self.pickerView.frame;
    framePicker.origin.y += PickerViewHeight;
    [UIView animateWithDuration:0.33 animations:^{
        [self.layer setOpacity:0];
        self.toolbar.frame = frameTool;
        self.pickerView.frame = framePicker;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

#pragma mark - --- delegate 视图委托 ---

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    
    return [self.dataArr count];
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return PickerViewLabelWeight;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    

    self.timeStr = self.dataArr [row];
    NSLog(@"%zd--%zd",row,component);
    
    
}

// 返回第component列第row行的标题
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
  
    return self.dataArr[row];
    
}




@end
